package com.tbfs.admin.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.tbfs.admin.service.sdk.ICompanyService;
import com.tbfs.admin.vo.Company;
import com.tbfs.common.BaseAction;

public class SaveCompanyAction extends BaseAction implements ModelDriven<Company>{
	
	private static final Logger logger = Logger.getLogger(SaveCompanyAction.class);
	
	private Company company = new Company();
	

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	
	private String message = null;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	// init service
	private ICompanyService companyService;
	
	// 构造函数
	public SaveCompanyAction(ICompanyService companyService) {
		this.companyService = companyService;
	}

	public SaveCompanyAction() {

	}
	
	
	/**
	 * save contractinfo data 
	 * @return
	 */
	public String saveData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin save companyinfo +++");
		}
		logger.info("companyName:"+company.getCompanyName());
		
		logger.info("id:"+company.getId());
		
		try {
			int id = company.getId()==null?0:company.getId();
			
			if(id>0){
				companyService.modify(company);
				company.setMessage("success");
			}else{
				companyService.add(company);
				company.setMessage("success");
			}
			
			
		} catch (Exception e) {
			company.setMessage("添加公司信息失败，请检查输入项目");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end save contractinfo +++");
		}
		return Action.SUCCESS;
	}
	
	public String destoryData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin delete contractinfo +++");
		}
		
		logger.info("id:"+company.getId());
		
		try {
		
			companyService.destory(company);
			
			company.setMessage("success");
			
		} catch (Exception e) {
			company.setMessage("删除公司信息失败，请检查输入项目");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end delete contractinfo +++");
		}
		return Action.SUCCESS;
	}

	public Company getModel() {
		// TODO Auto-generated method stub
		return company;
	}

}
